package com.example.myspringbootproject.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.myspringbootproject.pojo.T_user;
import com.example.myspringbootproject.service.impl.UserServiceImpl;
import com.example.myspringbootproject.util.Result;
import com.example.myspringbootproject.util.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/exam/user")
public class UserController {
    @Autowired
    UserServiceImpl userService;
    private T_user one;
    /**
     *  登录接口
     * @return
     */
    @PostMapping(value = "/login")
    public Result login(@RequestBody T_user t_user)
    {

        System.out.println("用户提交的数据是："+t_user);
        System.out.println("访问了登录的控制器");
        //去service完成操作
        QueryWrapper<T_user> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("loginname", t_user.getUsername());
        queryWrapper.or();// field1等于condition1
        queryWrapper.eq("email", t_user.getUsername());
        queryWrapper.or();
        queryWrapper.eq("tel", t_user.getUsername());// 且field2等于condition2
        one = userService.getOne(queryWrapper);
        //判断返回结果是否为空,如果是空说明没这个用户
        if (one != null){
            //判断密码是否正确
            if (one.getPassword().equals(t_user.getPassword())){
                //如果输入的密码与数据库的密码相符合，就成功登录
                Map<String,Object> map = new HashMap<String, Object>();
                map.put("token","admin-token");
                return Result.ok(map);
            }
        }
        //否则返回账号或者密码不正确
        return Result.build(null, ResultCodeEnum.LOGIN_MOBLE_ERROR);

    }



    @PostMapping(value = "/addUser")
    public Result addUser(@RequestBody T_user t_user){
        System.out.println("添加的数据是："+ t_user);
        //将添加对象的用户信息补充完整
        t_user.setLoginname(new SimpleDateFormat("YYYYMMdd").format(new Date())+ (new Random().nextInt(90000)+10000));//当前的时间年月日再跟上5位随机数
        t_user.setHeadImg("123456.png");
        t_user.setCreateTime(new Date());
        t_user.setType(1);
        //将添加对象的用户信息补充完整(写死)
        t_user.setRole("STUDENT");//学生表 默认身份
        if (ObjectUtils.isEmpty(t_user.getPassword())){//学生表 默认密码
            t_user.setPassword("123456");
        }
        t_user.setScore(0);//学生表 默认学分
        //执行添加操作
        boolean save = userService.save(t_user);
        return save?Result.ok():Result.fail();
    }

    //修改
    @PostMapping(value = "/updateUser")
    public Result updateUser(@RequestBody T_user t_user){
        System.out.println("修改的数据是："+ t_user);
        //将添加对象的用户信息补充完整
        boolean update = userService.updateById(t_user);
        //执行添加操作

        return update ? Result.ok():Result.fail();
    }


    /**
     * 获取用户列表
     */
    @GetMapping(value = "/list")

    public Result list(int currentPage,int pageSize,String content) {
        System.out.println("当前页："+currentPage+"查询条数："+pageSize+"查询条件"+content);
        //条件查询
        QueryWrapper<T_user> qw = new QueryWrapper<>();
        //判断是否按性别查询
        if (content.equals("男") || content.equals("女")){
            qw.eq("sex",content.equals("男")? 1 : 0);
        }else {
            qw.like("loginname", content).or().like("username",content).or().like("email",content).or().like("tel",content);
        }


        Page<T_user> page = new Page<>(currentPage,pageSize);
        IPage<T_user> page1 = userService.page(page,qw);


        Map<String, Object> map = new HashMap<>();
        map.put("list", page1.getRecords());
        map.put("total", page1.getTotal());

        return Result.ok(map);

    }
    /**
     *  获取用户信息
     */
    @GetMapping(value = "/info")
    public Result info()
    {
        Map<String,Object> map = new HashMap<>();
        map.put("roles","[admin]");
        map.put("introduction","I am a super admin");
        map.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        map.put("name",one.getUsername());
        return Result.ok(map);

    }


    @GetMapping (value = "/delete")
    public Result delete(@RequestParam List<Long> ids) {
        System.out.println("ids==="+ids);

        boolean b=userService.removeBatchByIds(ids);
        return b ? Result.ok() : Result.fail();

    }

    /**
     * 退出
     * @return
     */
    @PostMapping("/logout")
    public Result logout(){
        return Result.ok();
    }
}
